<?php

/*
 * This file is part of the FOSUserBundle package.
 *
 * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Gds\UserBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Symfony\Component\HttpFoundation\File\UploadedFile;

class RegistrationFormType extends AbstractType {

    private $class;

    /**
     * @param string $class The User class name
     */
    public function __construct($class, $roles_hierarchy = null) {
        $this->roles_hierarchy = $roles_hierarchy;
        $this->class = $class;
    }

    public function buildForm(FormBuilderInterface $builder, array $options) {
        $builder
                ->add('username', null, array('label' => 'Login'))
                ->add('email', 'email', array('label' => 'Email'))
                ->add('plainPassword', 'repeated', array(
                    'type' => 'password',
                    'options' => array('translation_domain' => 'FOSUserBundle'),
                    'first_options' => array('label' => 'Mot de passe'),
                    'second_options' => array('label' => 'Verifiaction'),
                    'invalid_message' => 'fos_user.password.mismatch',
                ))
                ->add('nom', 'text', array('label' => 'Prénom'))
                ->add('prenom', 'text', array('label' => 'Nom'))
                ->add('photo', 'text')
                ->add('telephone')
                ->add('cin', 'text', array('label' => 'Carte identité'))
                ->add('function', 'text', array('label' => 'Fonction'))
                ->add('departement',null,array('label' => 'Département'))
                ->add('roles', 'choice', array(
                    'choices' => array(
                        "ROLE_USER" => "ROLE_USER",
                        "ROLE_ADMIN" => "ROLE_ADMIN",
                        "ROLE_FOURNISSEUR_NEW" => "ROLE_FOURNISSEUR_NEW",
                        "ROLE_FOURNISSEUR" => "ROLE_FOURNISSEUR",
                        "ROLE_FOURNISSEUR_EDIT" => "ROLE_FOURNISSEUR_EDIT",
                        "ROLE_FOURNISSEUR_SHOW" => "ROLE_FOURNISSEUR_SHOW",
                        "ROLE_CLIENT_DELETE" => "ROLE_CLIENT_DELETE",
                        "ROLE_CLIENT_CRUD" => "ROLE_CLIENT_CRUD"
                    ),
                    'property_path' => false,
                    'multiple' => true,))
        ;
    }

    public function uploadAction() {
        if ($form->isValid()) {
            $extension = $file->guessExtension();
            if (!$extension) {
                // l'extension n'a pas été trouvée
                $extension = 'bin';
            }
            $file->move('/uploads/user/photo/', $form['username'] . rand(1, 99999) . '.' . $extension);
        }
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver) {
        $resolver->setDefaults(array(
            'data_class' => $this->class,
            'intention' => 'registration',
        ));
    }

    public function getName() {
        return 'gds_user_registration';
    }

}
